package com.li.controller;

import com.alibaba.fastjson.JSONObject;
import com.li.domain.Advertisement;
import com.li.service.AdvertisementService;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {

    @Autowired
    private AdvertisementService advertisementService;

    @RequestMapping("/listPageAdvertisement")
    public JSONObject listPageAdvertisement(Advertisement advertisement,Integer page,Integer limit){
        List<Advertisement> list=advertisementService.listPageAdvertisement(advertisement,page,limit);
        Integer count = advertisementService.listPageAdvertisementCount(advertisement);
        return R.listPage(list,count);
    }

    /**
     * 图片上传
     * @param uploadFile
     * @param req
     * @return
     */
    @RequestMapping("/uploadImage")
    public String uploadAvatar(MultipartFile uploadFile,String remark, HttpServletRequest req){
        String path=null;
        try {
            path=advertisementService.uploadImage(uploadFile,remark , req);
        }catch (Exception e){
            return null;
        }
        return path;
    }

    /**
     * 修该
     * @param advertisement
     * @return
     */
    @RequestMapping("/updateAdvertiseent")
    public JSONObject updateAdvertiseent(Advertisement advertisement){
        try{
            advertisementService.updateById(advertisement);
        }catch (Exception e){
            return R.getError(advertisement);
        }
        return R.getSuccess();
    }

    /**
     * 删除一条
     * @param id
     * @return
     */
    @GetMapping("/delAdvertisement/{id}")
    public JSONObject delAdvertisement(@PathVariable("id") Integer id){
        try{
            Advertisement advertisement = advertisementService.getById(id);
            if (advertisement==null){
                return R.getError(id);
            }
            String filePath=advertisement.getFilepath();
            File file=new File(filePath);
            //如果这个文件物理存在
            if (file.exists()){
                file.delete();//删除文件
            }
            advertisementService.removeById(id);
        }catch (Exception e){
            return R.getError(id);
        }
        return R.getSuccess();
    }

    /**
     * 批量删除
     * @param advertisement
     * @return
     */
    @RequestMapping("/delAdvertisements")
    public JSONObject delAdvertisements(Advertisement advertisement){
        try{
            advertisementService.delAdvertisements(advertisement.getIds());
        }catch (Exception e){
            return R.getError(advertisement);
        }
        return R.getSuccess();
    }
}
